package com.epalmpay.job;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

import com.alibaba.fastjson.JSONObject;
import com.epalmpay.entity.Coupon;
import com.epalmpay.entity.YsOilcardRechargeInfo;
import com.epalmpay.enumdef.CommonEnum.RunStatus;
import com.epalmpay.enumdef.CommonEnum.TaskType;
import com.epalmpay.service.activity.IOtherCouponService;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.util.DateUtil;

public class CouponCreateTaskJob {
	
	private Logger logger = LoggerFactory.getLogger(OilcardChargeTaskJob.class);
	
	@Resource
	private ISysLogService sysLogService;
	
	@Resource
	private IOtherCouponService otherCouponService;
	/**
	 * 5点执行所有未生成券码的订单
	 */
	@Scheduled(cron="0 0 5 * * ?")
	public void couponCreateTask(){
		logger.info("====couponCreateTask==============================================================="); 
    	logger.info("进行执行  券码没有生成的订单"); 
    	List<Coupon> coupons= otherCouponService.findNeedCreateCodeInfo();
		for(Coupon coupon:coupons){
			try {
			    otherCouponService.finishCreateCodeInfo(coupon.getId());
			    
			  } catch (Exception e) {
				e.printStackTrace();
				StringBuffer sb = new StringBuffer(); 
				try {
					sysLogService.recordSysTaskLog(TaskType.couponCodeCreate.getType(),
							new Date(), RunStatus.Fail.getType(),
							sb.append("优惠券订单Id:").append(coupon.getId()).append("优惠券生成 券码失败！").toString());
				} catch (Exception e1) {
					e1.printStackTrace();
					continue;
				}
			}	
		}
		StringBuffer sb = new StringBuffer(); 
		logger.info(sb.append("计划任务:")
				.append(TaskType.couponCodeCreate.getName())
				.append("执行日期:").append(DateUtil.fomatDate(new Date(), DateUtil.sdfDays)).append("执行成功！").toString());
		try {
			sysLogService.recordSysTaskLog(TaskType.couponCodeCreate.getType(),
					new Date(), RunStatus.Success.getType(),
					sb.append("优惠券生成 券码").append(DateUtil.fomatDate(new Date(), DateUtil.sdfTime)).append("执行成功！").toString());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
    	logger.info("=======================================couponCreateTask end============================="); 
	}
}
